package com.aohanyao.product.flavorsdemos;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.TextView;

public class HorizontalWrapLineLayout extends ViewGroup {
    private int mVerticalSpace = 0;
    private int mHorizontalSpace = 0;
    private Context mContext;

    public HorizontalWrapLineLayout(Context context) {
        this(context, null);mContext = context;
    }

    public HorizontalWrapLineLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
        mContext = context;
    }

    private void init(Context context, AttributeSet attrs) {
        setAttributeSet(context, attrs);mContext = context;
    }

    public void setText(String text, final Animation animation, int duration) {
        int time = 0;
        if(text != null && !text.isEmpty()) {
            char[] characters = text.toCharArray();
            for(char c : characters) {
                final TextView t = new TextView(mContext);
                //遍历传入的字符串的每个字符，生成一个TextView，并设置它的动画
                t.setText(String.valueOf(c));
                t.setTextSize(28);
                Handler h = new Handler();
                //每隔duration时间，播放下一个TextView的动画
                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        addView(t);
                        t.setAnimation(animation);
                    }
                }, time);

                time += duration;

            }
        }
    }

    /**
     * 设置自定义属性
     *
     * @param context
     * @param attrs
     */
    private void setAttributeSet(Context context, AttributeSet attrs) {
//        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HorizontalWrapLineLayout);
        //属性中定义左右边距 padding margin
        //属性中定义每一个距离垂直方向vertalSpace 和水平方向horizontalSpace
//        mVerticalSpace = (int) a.getDimension(R.styleable.HorizontalWrapLineLayout_verticalWrapSpace, 0);
//        mHorizontalSpace = (int) a.getDimension(R.styleable.HorizontalWrapLineLayout_horizontalWrapSpace, 0);
//        a.recycle();

    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected boolean checkLayoutParams(LayoutParams p) {
        return p instanceof MarginLayoutParams;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 这里的高度和宽度是去掉margin的值
        int horizontalPadding = getPaddingLeft() + getPaddingRight();
        int measureWidth = horizontalPadding;
        int verticalPadding = getPaddingTop() + getPaddingBottom();
        int measureHeight = verticalPadding;
        final int childCount = getChildCount();

        int widthSpaceSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpaceMode = MeasureSpec.getMode(heightMeasureSpec);
        if (childCount == 0) {
            setMeasuredDimension(0, 0);
            return;
        }
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            //测量子View
            if (childView.getVisibility() != View.GONE) {
                measureChildWithMargins(childView, widthMeasureSpec, 0, heightMeasureSpec, 0);
                MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams();
                int childMeasuredHeight = childView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
                int childMeasuredWidth = childView.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
                measureWidth += childMeasuredWidth + mHorizontalSpace;
                if (measureWidth > widthSpaceSize) {
                    measureHeight += childMeasuredHeight + mVerticalSpace;
                    measureWidth = getPaddingLeft() + getPaddingRight();
                }
                if (childCount - 1 == i && measureWidth > 0) {
                    measureHeight += childMeasuredHeight + mVerticalSpace;
                }
            }
        }
        if (heightSpaceMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthMeasureSpec, measureHeight);
        } else {
            setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        }
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //这里的四角参数，是去掉Margin的参数
        int childCount = getChildCount();
        int left = getPaddingLeft();
        int top = getPaddingTop();
        int right = r - getPaddingRight();

        int currentLeft = left;
        int currentTop = top;
        int lineHeight = 0;
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams();
            //确定4个点
            if (currentLeft + childView.getMeasuredWidth() + lp.leftMargin + lp.rightMargin >= right) {//换行
                currentLeft = left;
                currentTop += lineHeight + mVerticalSpace;
                lineHeight = childView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            } else {
                lineHeight = Math.max(lineHeight, childView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
            }
            childView.layout(
                    currentLeft + lp.leftMargin,
                    currentTop + lp.topMargin,
                    currentLeft + lp.leftMargin + childView.getMeasuredWidth(),
                    currentTop + lp.topMargin + childView.getMeasuredHeight());
            currentLeft += childView.getMeasuredWidth() + lp.leftMargin + lp.rightMargin + mHorizontalSpace;
        }
    }
}
